import Vue from 'vue'
import store from '@/store'

//数据字典名称转换
Vue.filter('dictLabel', (val, dictKey, placeholder = '-') => {
  if (val !== '') {
    if (dictKey && store.getters.dictionary.hasOwnProperty(dictKey)
      && typeof (store.getters.dictionary[dictKey][val]) !== "undefined") {
      return store.getters.dictionary[dictKey][val]
    } else {
      return val
    }
  } else {
    return placeholder
  }
});
//数据字典名称转换
Vue.filter('sizeFilter', (val) => {
  var size = "";
  if (!val) {
    return ''
  }
  if (val < 0.1 * 1024) { //如果小于0.1KB转化成B  
    size = val.toFixed(2) + "B";
  } else if (val < 0.1 * 1024 * 1024) { //如果小于0.1MB转化成KB  
    size = (val / 1024).toFixed(2) + "KB";
  } else if (val < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB  
    size = (val / (1024 * 1024)).toFixed(2) + "MB";
  } else if (val < 0.1 * 1024 * 1024 * 1024 * 1024) { //如果小于0.1TB转化成GB  
    size = (val / (1024 * 1024 * 1024)).toFixed(2) + "GB";
  } else { //其他转化成TB
    size = (val / (1024 * 1024 * 1024 * 1024)).toFixed(2) + "TB";
  }
  var sizestr = size + "";
  var len = sizestr.indexOf("\.");
  var dec = sizestr.substr(len + 1, 2);
  if (dec == "00") { //当小数点后为00时 去掉小数部分  
    return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
  }
  return sizestr;
});

//设置只显示几个字符串，默认20个
Vue.filter('filterAmount', function (value, n) {
  if (!n) n = 20;
  if (value && value.length > n) {
    value = value.substring(0, n) + '...';
  }
  return value;
}
)
